module.exports = {
  // 清空通讯录所有联系人
  cleanContact: () => {
    var ContentProviderOperation = android.content.ContentProviderOperation;
    var rawUri = android.provider.ContactsContract.Data.CONTENT_URI.buildUpon()
      .appendQueryParameter("caller_is_syncadapter", "true")
      .build();
    var ops = new java.util.ArrayList();
    var array = java.lang.reflect.Array.newInstance(java.lang.String, 1);
    array[0] = "-1";
    ops.add(
      ContentProviderOperation.newDelete(android.provider.ContactsContract.Data.CONTENT_URI)
        .withSelection("_id>? ", array)
        .build()
    ); //sets deleted flag to 1
    ops.add(ContentProviderOperation.newDelete(rawUri).withSelection("_id>? ", array).build()); //erases
    context.getContentResolver().applyBatch("com.android.contacts", ops);
  },

  // 写入通讯录单个联系人
  addContact: (name, phone) => {
    var a = new android.content.ContentValues();
    a.put("account_type", android.accounts.AccountManager.KEY_ACCOUNT_TYPE);
    a.put("account_name", android.accounts.AccountManager.KEY_ACCOUNT_NAME);

    var rawContactUri = context.getContentResolver().insert(android.provider.ContactsContract.RawContacts.CONTENT_URI, a);
    var rawContactId = android.content.ContentUris.parseId(rawContactUri);

    var b = new android.content.ContentValues();
    b["put(java.lang.String,java.lang.Long)"]("raw_contact_id", rawContactId);
    b.put("mimetype", "vnd.android.cursor.item/name");
    b.put("data1", name);
    context.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, b);

    var c = new android.content.ContentValues();
    c["put(java.lang.String,java.lang.Long)"]("raw_contact_id", rawContactId);
    c.put("mimetype", "vnd.android.cursor.item/phone_v2");
    c.put("data1", phone);
    c["put(java.lang.String,java.lang.Integer)"]("data2", 2);
    context.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, c);
  },
};
